package com.android.vgongyi.dao;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.android.vgongyi.common.AppConstant;
import com.android.vgongyi.entity.ImageEntity;

public class ImageDAO extends BaseDAO{
	
	private Context context;
	
	private String imageUrl  = "imageUrl";
	private String imagepath = "imagepath";
	private String imageTime = "imageTime";
	
	public ImageDAO(Context context) {
		
		this.context = context;
		
	}
	
	@Override
	public String createTableString() {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("create table ");
		buffer.append(TABLE_IMAGE);
		buffer.append("(");
		buffer.append("_id integer primary key AUTOINCREMENT,");
		buffer.append(imageUrl).append(" text unique,");
		buffer.append(imagepath).append(" text,");
		buffer.append(imageTime).append(" text");
		buffer.append(");");
		
		return buffer.toString();
	}

	@Override
	public String dropTable() {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(super.DROP_TABLE);
		buffer.append(TABLE_IMAGE);
		
		return buffer.toString();
	}

	@Override
	public void insert(Object obj) {
		
		ImageEntity entity		 = (ImageEntity)obj;
		ContentResolver resolver = context.getContentResolver();
		ContentValues values 	 = new ContentValues();
		
		values.put(imageUrl, entity.getImageUrl());
		values.put(imagepath, entity.getImagepath());
		values.put(imageTime, entity.getImageTime());
		
		resolver.insert(URI_IMAGE, values);		
		
	}

	@Override
	public void delete(String where, String[] selectionArgs) {
		
		ContentResolver resolver = context.getContentResolver();
		resolver.delete(URI_IMAGE, where, selectionArgs);
		
	}

	@Override
	public ArrayList<ImageEntity> query(String[] selections, String where,String[] selectionArgs,String sortOrder) {
		
		ContentResolver resolver 		 = context.getContentResolver();
		Cursor cursor 					 = resolver.query(URI_IMAGE, selections, where, selectionArgs, sortOrder);
		ArrayList<ImageEntity> arrayList = new ArrayList<ImageEntity>();	
		ImageEntity entity				 = null;
		
		if(null==cursor)  return arrayList;
		
		if(cursor.getCount()<AppConstant.NO_1){
			
			cursor.close();
			return arrayList;
			
		}
		
		int count = cursor.getCount();
		
		if(count>AppConstant.NO_0)  cursor.moveToFirst();
		
		do {
			
			entity = new ImageEntity();
			entity.setImageUrl(cursor.getString(cursor.getColumnIndex(imageUrl)));
			entity.setImagepath(cursor.getString(cursor.getColumnIndex(imagepath)));
			entity.setImageTime(cursor.getString(cursor.getColumnIndex(imageTime)));
			arrayList.add(entity);
			
		} while (cursor.moveToNext());
		
		cursor.close();
		
		return arrayList;
	}

	@Override
	public void update(Object obj, String where, String[] selectionArgs) {
		
		ImageEntity entity  	 = (ImageEntity )obj;
		ContentResolver resolver = context.getContentResolver();
		ContentValues values     = new ContentValues();
		
		values.put(imageUrl, entity.getImageUrl());
		values.put(imagepath, entity.getImagepath());
		values.put(imageTime, entity.getImageTime());
		
		resolver.update(URI_IMAGE, values, where, selectionArgs);
		
	}

}
